<template>
  <div>
    <div class="permissManaTool" style="text-align: left;">
      <el-input placeholder="请输入角色英文名" v-model="role.name">
        <template slot="prepend">ROLE_</template>
      </el-input>
      <el-input placeholder="请输入角色中文名" v-model="role.nameZh" @keydown.enter.native="doAddRole"></el-input>
      <el-button @click="doAddRole" type="primary" icon="el-icon-plus">添加角色</el-button>
    </div>
    <div class="permissManaMain">
      <el-collapse accordion @change="change">
        <el-collapse-item :title="r.nameZh" :name="r.id" v-for="
(r,index) in roles" :key="index">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>可访问资源</span>
              <el-button
                style="float: right; padding: 3px
0;color: #ff0000"
                @click="doDeleteRole(r)"
                icon="el-icon-delete"
                type="text"
              ></el-button>
            </div>
            <div>
              <el-tree
                show-checkbox
                node-key="id"
                :key="index"
                :default-checked-keys="selectedMenus"
                :data="allMenus"
                :props="defaultProps"
                ref="tree"
              ></el-tree>
              <div style="display: flex;justify-content: flex-end">
                <el-button @click="cancelUpdate">取消修改</el-button>
                <el-button type="primary" @click="doUpdate(r.id,index)">确认修改</el-button>
              </div>
            </div>
          </el-card>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>

<script>
export default {
  name: 'PermissMana',
  data () {
    return {
      role: {
        name: '',
        nameZh: ''
      },
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      activeName: ['2'],
      selectedMenus: [],
      allMenus: [],
      roles: []
    }
  },
  mounted () {
    this.initRoles()
  },
  methods: {
    doDeleteRole (role) {
      this.$confirm('此操作将永久删除该【' + role.nameZh + '】角色, 是否继续 ? ', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/system/basic/permiss/role/' + role.id).then(resp => {
          if (resp) {
            this.initRoles()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    doAddRole () {
      if (this.role.name && this.role.nameZh) {
        this.postRequest('/system/basic/permiss/role/', this.role).then(resp => {
          if (resp) {
            this.initRoles()
            this.name = ''
            this.nameZh = ''
          }
        })
      } else {
        this.$message.error('字段不能为空!')
      }
    },
    cancelUpdate () {
      this.activeName = -1
    },
    doUpdate (rid, index) {
      const tree = this.$refs.tree[index]
      const selectedKeys = tree.getCheckedKeys(true)
      let url = '/system/basic/permiss/?rid=' + rid
      selectedKeys.forEach(key => {
        url += '&mids=' + key
      })
      // console.log(url)
      this.putRequest(url).then(resp => {
        if (resp) {
          this.initRoles()
          this.activeName = -1
        }
      })
    },

    change (rid) {
      if (rid) {
        this.initAllMenus()
        this.initSelectedMenus(rid)
      }
    },
    initSelectedMenus (rid) {
      this.getRequest('/system/basic/permiss/mid/' + rid).then(resp => {
        this.selectedMenus = resp
      })
    },
    initAllMenus () {
      this.getRequest('/system/basic/permiss/menus').then(resp => {
        this.allMenus = resp
      })
    },
    initRoles () {
      this.getRequest('/system/basic/permiss/').then(resp => {
        if (resp) {
          this.roles = resp
        }
      })
    }
  }
}
</script>

<style scoped>
.permissManaTool {
  display: flex;
  justify-content: flex-start;
}
.permissManaTool .el-input {
  width: 300px;
  margin-right: 6px;
}
.permissManaMain {
  margin-top: 10px;
  width: 700px;
}
</style>
